package com.shi.yoyoshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shi.yoyoshop.bean.Goods;
import com.shi.yoyoshop.mapper.GoodsMapper;
import com.shi.yoyoshop.service.GoodsService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 20556
 * @description 针对表【goods】的数据库操作Service实现
 * @createDate 2024-01-04 15:06:01
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
        implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public List<Goods> getGoodsListByHotTypeId(Integer typeId) {
        return goodsMapper.selectGoodsListByHotTypeId(typeId);
    }

    @Override
    public Page<Goods> getGoodsListByHotTypeIdWithPage(Page<Goods> page, Integer typeId) {

        List<Goods> goodsList = goodsMapper.selectGoodsListByHotTypeIdWithPage(typeId, (int) ((page.getCurrent() - 1) * page.getSize()), (int) page.getSize());
        Long total = goodsMapper.selectGoodsCountByHotTypeId(typeId);
        page.setRecords(goodsList);
        page.setTotal(total);
        return page;
    }


    @Override
    public Goods getGoodsAndTypeByGoodsId(Integer goodId) {
        return goodsMapper.selectGoodsAndTypeByGoodsId(goodId);
    }

    @Override
    public Page<Goods> getGoodsListByTypeIdWithPage(Page<Goods> page, Integer typeId) {
        return goodsMapper.selectPage(page, new LambdaQueryWrapper<Goods>()
                .select(Goods::getId, Goods::getName, Goods::getPrice, Goods::getIntro, Goods::getCover, Goods::getImage1, Goods::getImage2, Goods::getStock, Goods::getTypeId, Goods::getStatus)
                .eq(Goods::getTypeId, typeId)
                .orderByDesc(Goods::getId));
    }

    @Override
    public Goods getGoodsById(Integer goodsId) {
        return goodsMapper.selectOne(new LambdaQueryWrapper<Goods>()
                .select(Goods::getId, Goods::getName, Goods::getPrice, Goods::getIntro, Goods::getCover, Goods::getImage1, Goods::getImage2, Goods::getStock, Goods::getTypeId, Goods::getStatus)
                .eq(Goods::getStatus, 1)
                .eq(Goods::getId, goodsId));
    }

    @Override
    public Page<Goods> getGoodsListByNameLikeWithPage(Page<Goods> goodsPage, String name) {
        return goodsMapper.selectPage(goodsPage, new LambdaQueryWrapper<Goods>()
                .select(Goods::getId, Goods::getName, Goods::getPrice, Goods::getIntro, Goods::getCover, Goods::getImage1, Goods::getImage2, Goods::getStock, Goods::getTypeId, Goods::getStatus)
                .like(!ObjectUtils.isEmpty(name), Goods::getName, name)
                .orderByDesc(Goods::getId));
    }

    @Override
    public Page<Goods> getGoodsListWithPage(Page<Goods> page) {
        return goodsMapper.selectPage(page, new LambdaQueryWrapper<Goods>()
                .select(Goods::getId, Goods::getName, Goods::getPrice, Goods::getIntro, Goods::getCover, Goods::getImage1, Goods::getImage2, Goods::getStock, Goods::getTypeId, Goods::getStatus)
                .eq(Goods::getStatus, 1)
                .orderByDesc(Goods::getId));
    }

    @Override
    public void removeGoodsById(Integer id) {
        goodsMapper.update(new Goods(),new LambdaUpdateWrapper<Goods>().set(Goods::getStatus, 0).eq(Goods::getId, id));
    }

    @Override
    public void modifyGoodsById(Goods goods) {
        goodsMapper.updateGoodsById(goods);
    }

    @Override
    public List<Goods> getGoodsListByTypeId(Integer id) {
        return goodsMapper.selectList(new LambdaQueryWrapper<Goods>()
                .select(Goods::getId)
                .eq(Goods::getTypeId, id)
                .eq(Goods::getStatus, 1));
    }

    @Override
    public List<Goods> getGoodsList() {
        return goodsMapper.selectList(new LambdaQueryWrapper<Goods>()
                .select(Goods::getId, Goods::getName, Goods::getPrice, Goods::getIntro, Goods::getCover, Goods::getImage1, Goods::getImage2, Goods::getStock, Goods::getTypeId, Goods::getStatus)
                .eq(Goods::getStatus, 1));
    }


}




